# ======================================================================
#
# Copyright (C) ADN
# ADN::LIBRARY::SNMP::Discovery Discovery.pm
# Description:
#  - Recherche d'équipement présent sur le réseau 
#
# ======================================================================

package ADN::LIBRARY::SNMP::Discovery;

use strict;
use ADN::LIBRARY::SNMP::Gestion;
use ADN::LIBRARY::PING::Ping;
use ADN::XML::Create;
use ADN::CONF::Read;
use ADN::LIBRARY::SOAP::ClientSOAP;

# #######################
# CREAT OBJECT SELF + INIT. VAR.
# #######################
sub new
{
  my $self = shift;
 	my $class = ref($self) || $self;
 	return $self if ref $self;
 	$self = bless {} => $class;
 	my $conf = ADN::CONF::Read->new();
  if ($conf =~ m/^[-]1$/smx)
  {
    return -1;
  }
  my %Disc = $conf->PING();
  $self->{boucle_ping} = $Disc{boucle};
  $self->_init();
  undef $conf;
  return ($self);
}

# #######################
# INIT.
# #######################
sub _init
{
  my $self = shift;

  $self->{BASE_OID} = '.1.3.6.1.2.1';
  %{$self->{SYSTEM}} = (
         SystemeDescription     => '.1.3.6.1.2.1.1.1.0',
         SystemeUptime          => '.1.3.6.1.2.1.1.3.0',
	       SystemeContact         => '.1.3.6.1.2.1.1.4.0',
	       SystemeName            => '.1.3.6.1.2.1.1.5.0',
	       SystemeLocation        => '.1.3.6.1.2.1.1.6.0',
	       NbInterface            => '.1.3.6.1.2.1.2.1.0',
	       ip_local               => '.1.3.6.1.2.1.4.20.1.1');
  $self->{requete} = ADN::LIBRARY::SNMP::Gestion->new($self->{BASE_OID}, \%{$self->{SYSTEM}});
  return;
}

# #######################
# LIST EQUIPEMENT SNMP
# #######################
sub start
{
  my $self = shift;
  my ($send);
  ($self->{ip}, $self->{ip_soap}, $self->{Srep}) = @_;

  $self->boucle_ping();
  if ((scalar (@{$self->{HOST}})) >= 1)
  {
    foreach my $host (@{$self->{HOST}})
    {
      if ($self->verif_ip_doublon($host))
      {
        if ($self->{requete}->start($host) !~ m/^0$/smx)
        {
          %{$self->{result}} = $self->{requete}->start($host);
					if ($self->verif_broadcast($host))
          {
            $self->tab_ip();
            $send = ADN::XML::Create->new(\%{$self->{result}}, $host);
            @{$self->{tab_xml}} = $send->Composant();
						$self->string();
            $self->send_clientSOAP();
          }
        }
      }
    }
  }
  $self->{xml} = '';
  $self->send_clientSOAP();
  return;
}

# #######################
# SEND TO SOAP
# #######################
sub send_clientSOAP
{
  my $self = shift;
  my @tab;

  print "\n\nDATA : $self->{xml}";
  print "\n\nIP : $self->{ip_soap}\nRep : $self->{Srep}";

  $tab[0] = $self->{xml};
  ADN::LIBRARY::SOAP::ClientSOAP->new( Proxy => $self->{ip_soap},
						                           Service => $self->{Srep},
						                           Data =>  \@tab );
	return;
}

# #######################
# TO string
# #######################
sub string
{
  my $self = shift;

  $self->{xml} = '';
  foreach my $ligne (@{$self->{tab_xml}})
  {
    $self->{xml} .= $ligne;
  }
  return;
}

# #######################
# CHECK ADDRESS BROADCAST
# AND NO SNMP
# #######################
sub verif_broadcast
{
  my $self = shift;
  my $ok = 0;

  while (my ($k, $v) = each(%{$self->{result}}))
  {
     if ($k =~ m/^ip_local\./sxm)
     {
        if ($_[0] =~ /^$v$/smx)
        {
          $ok = 1;
        }
     }
  }
  return $ok;
}

# #######################
# ARRAY OF LOCAL IP EQUIPEMENT
# TO AVOID DOUBLE
# #######################
sub tab_ip
{
  my $self = shift;
  my ($host, $k, $v);
  $self->{listeip_liens} = '';

  while (($k, $v) = each(%{$self->{result}}))
  {
     if ($k =~ m/^ip_local\./smx)
     {
        foreach $host (@{$self->{HOST}})
        {
					if ($host =~ m/^$v$/smx)
					{
					  $self->{listeip_liens} .= "[{$v}],";
					  push @{$self->{recup_ip}}, $v;
					}
				}
      	delete $self->{result}{$k};
     }
  }
  $self->{listeip_liens} =~ s/,$//smx;
  $self->{result}{listeip_liens} = $self->{listeip_liens};
  undef $self->{listeip_liens};
  delete $self->{result}{ip_local};
  return;
}

# #######################
# CHECK IP DOUBLE
# #######################
sub verif_ip_doublon
{
  my $self = shift;

  foreach my $ligne (@{$self->{recup_ip}})
  {
     if ($_[0] =~ m/^$ligne$/smx)
     {
        undef $ligne;
        return 0;
     }
  }
  return 1;
}

# #######################
# SET PING
# #######################
sub boucle_ping
{
  my $self = shift;
  my $boucle = $self->{boucle_ping};
  $self->{ping} = ADN::LIBRARY::PING::Ping->new();

  while ($boucle != 0)
  {
    $self->{ping}->start(@{$self->{ip}});
    $boucle--;
  }
  undef $boucle;
  @{$self->{HOST}} = $self->{ping}->start(@{$self->{ip}});
  undef $self->{ping};
  undef @{$self->{ip}};
  return;
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
    my $self = shift;
    return;
}
1;
__END__
